<script>
import LoginServiceApi from '@/views/auth/login/assets/loginServiceApi'
import { getHashSearchParam, getQueryVariable, setToken } from '@/token'
import { mapActions } from 'vuex'
import { flattenTree } from '@/utils'
import router from '@/router'

export default {
  name: '',
  methods: {
    ...mapActions('loginStore', {
      fetchUserInfo: 'fetchUserInfo'
    }),
    async digitalBaseSso(code = '') {
      try {
        const { data } = await LoginServiceApi.digitalBaseSso(code)
        setToken(data?.access_token)
        const [userInfo, routers] = await this.fetchUserInfo()

        const defaultUrl = flattenTree(routers).filter(v => v.query && v.query === 'menu')[0]?.path
        if (defaultUrl) {
          router.push(defaultUrl)
        } else {
          router.push({
            name: 'Welcome'
          })
        }
      } catch (e) {
        router.push({
          name: 'Login'
        })
      }
    }
  },
  mounted() {
    debugger
    const code = getHashSearchParam('code') || getQueryVariable('code') || ''
    this.digitalBaseSso(code)
  }
}
</script>

<template>
  <div></div>
</template>

<style scoped lang="scss"></style>
